<?php 


/**
 * 腾讯点播Sdk
 */
class tcplayer
{
	
	public $secret_id;
	public $secret_key;
	public $Host = 'https://vod.api.qcloud.com/v2/index.php';
	public $ErrorNum = 0;


	function __construct($secret_id,$secret_key)
	{
		$this->secret_id = $secret_id;
		$this->secret_key = $secret_key;
	}

	/**
	 * [get_video_info 获取视频信息资料]
	 * @param  [int]  $fileId      [视频文件id]
	 * @param  [Array] $infoFilter [获取资料类型]
	 * @return [Array]              [视频资料信息]
	 */
	public function get_video_info($fileId, $infoFilter=array()){
		$Param = array('Action'=>'GetVideoInfo','fileId'=>$fileId);
		$i = 0;
		foreach ($infoFilter as $k => $v) {
			$Param['infoFilter.'.$i] = $v;
			$i++;
		}
		$ret = json_decode($this->request_api($this->Get_Api_Sign_SrcUrl($Param)),TRUE);
		if ($ret['code'] <> 0 and $this->ErrorNum < 3) {
			$this->ErrorNum++;
			$this->get_video_info($fileId, $infoFilter);
		}else{
			$this->ErrorNum = 0;
			return $ret;
		}
		return $ret;
	}

	/**
	 * [video_to_m3u8 对时间进行m3u8转码]
	 * @param  [int]  $fileId       [视频文件ID]
	 * @param  [integer] $isScreenshot [是否截图]
	 * @param  [integer] $isWatermark  [是否追加水印]
	 * @return [array]                [返回处理结果]
	 */
	public function video_to_m3u8($fileId, $isScreenshot=0, $isWatermark=0){
		$Param = array('fileId'=>$fileId,'Action'=>'ConvertVodFile','isWatermark'=>$isWatermark,'isScreenshot'=>$isScreenshot);
		$url = $this->Get_Api_Sign_SrcUrl($Param);
		$ret = json_decode($this->request_api($url),TRUE);
		if ($ret['code'] <> 0 and $this->ErrorNum < 3) {
			$this->ErrorNum++;
			$this->video_to_m3u8($fileId, $isScreenshot, $isWatermark);
		}else{
			$this->ErrorNum = 0;
			return $ret;
		}
	}

	/**
	 * [api_common_Param 接口参数结合公共参数]
	 * @param  [array] $append_param [接口参数]
	 * @return [array]               [结合成功后的参数]
	 */
	public function api_common_Param($append_param){
		$Param = array('Region'=>'cd','Timestamp'=>time(),'Nonce'=>time()+rand(100,9999),'SecretId'=>$this->secret_id);
		foreach ($append_param as $k => $v) {
			$Param[$k] = $v;
		}
		ksort($Param);
		return $Param;
	}

	/**
	 * [Get_Api_Sign_SrcUrl 获取组合好的接口api完整地址]
	 * @param [String] $host    [接口地址]
	 * @param [Array] $option  [请求参数]
	 * @param string $reqType [返回完整的API请求地址]
	 */
	public function Get_Api_Sign_SrcUrl($option, $reqType='GET'){
		$host = $this->Host;
		preg_match('/https\:\/\//',$host,$hostArr);
		if ($hostArr) {
			$host = str_replace('https://',$reqType,$host);
			$host_header = 'https://';
		}
		preg_match('/http\:\/\//',$host,$hostArr);
		if ($hostArr) {
			$host = str_replace('https://',$reqType,$host);
			$host_header = 'http://';
		}

		$Param = $this->api_common_Param($option);
		foreach ($Param as $k => $v) {
			@$_get .= $_get?'&'.$k.'='.$v:'?'.$k.'='.$v;
		}

		$srcStr = $host.$_get;
		$signStr = base64_encode(hash_hmac('sha1', $srcStr, $this->secret_key, true));
		$srcStr .= '&Signature='.$signStr;
		$srcStr = str_replace($reqType,$host_header,$srcStr);
		return $srcStr;
	}

	/**
	 * [create_upload_sign 生成上传视频签名]
	 * @return [string] [上传签名]
	 */
	public function create_upload_sign(){

		// 确定签名的当前时间和失效时间
		$current = time();
		$expired = $current + 86400;  // 签名有效期：1天

		// 向参数列表填入参数
		$arg_list = array(
		    "secretId" => $this->secret_id,
		    "currentTimeStamp" => $current,
		    "expireTime" => $expired,
		    "random" => rand());

		// 计算签名
		$orignal = http_build_query($arg_list);
		$signature = base64_encode(hash_hmac('SHA1', $orignal, $this->secret_key, true).$orignal);
		return $signature;
	}

	/**
	 * [request_api 通过Curl异步请求API接口]
	 * @param  [String] $url [Api地址]
	 * @return [json]      [返回的结果值]
	 */
	public function request_api($url)   
	{  
	    $ch = curl_init();  
	    curl_setopt($ch, CURLOPT_URL,$url);  //设置访问的url地址     
	    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
	    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);      
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//不输出内容  
	    $result =  curl_exec($ch);  
	    curl_close ($ch);  
	    return $result;   
	}  


}


?>